import {searchPage,searchOrderDetail,searchOne,approvalApply,applyToInorder} from '../../../api/b2cReturns'

let approval = {
    data() {
        return {
            //日期组件
            createTimeOptions:'',
            approvalTimeOptions:'',
            //table的list
            tableData: [],
            total:0,
            searchParams:{
                currentPage :1,
                pageSize:5,
                approvalStatus:0
            },
            approvalStatusOptions:[
                {
                    value:null,
                    label:"全部"
                },
                {
                    value: 0,
                    label: '未审批'
                }, {
                    value: 1,
                    label: '已通过'
                }, {
                    value:2,
                    label: '未通过'
                }, {
                    value:3,
                    label: '已入库'
                }
            ],
            goodStatusOptions:[
                {
                    value: 0,
                    label: '退货'
                }, {
                    value: 1,
                    label: '同机型换货'
                }, {
                    value:2,
                    label: '不同机型换货'
                }
            ],
            dialogVisible:false,
            formData:{},
            editFormVisible:false,
            editApplyForm:{
                applyId:'',
                sellCode:'',
                complaintId:'',
                producer:'admin',
                producDept:'售后部门',
                remarks:'',
                status:0,
                overhaulId:'',
                overhaulName:'',
                overhaulTime:'',
                overhaulPhone:'',
                approver:'审核人',
                approvalTime:'',
                approvalIdea:'',
                approvalStatus:0,
                createTime:null,
                entryId:'',
                list:[],
                listNew:[]
            },
            orderData:{
                orderCode:'',
                orderType:'',
                orderAction:'',
                businessType:'',
                orderState:'',
                orderCreateTime:'',
                consigneeName:'',
                memberAccount:'',
                phoneNumber:'',
                cellphoneNumber:'' ,
                postalCode:'',
                deliveryAddress:'',
                deliveryArea:'',
                deliveryWay:'' ,
                deliveryCost:'',
                payWay:'',
                selfPickupTime:'',
                email:'',
                payer:'',
                remark:'',
                sellOrderGoodsList:[],
            },
            editForm:true,
            title:'',
            inorderList:[]
        };
    },
    methods:{
        async orderDetail(sellCode){
            let response =  await searchOrderDetail(sellCode);
            this.orderData = JSON.parse(JSON.stringify(response));
            this.orderData.orderCreateTime = this.changeDate(this.orderData.orderCreateTime);
            this.orderData.selfPickupTime = this.changeDate(this.orderData.selfPickupTime);
        },
        //审核
        async applyApproval(apply){
            let response =await searchOne(apply.applyId);
            this.editForm = false;
            this.editFormVisible = true;
            this.title="审核申请单";
            this.editApplyForm=JSON.parse(JSON.stringify(response));
            if(response.sellCode!==null){
                this.orderDetail(response.sellCode);
            }
        },
        async updateApply(approvalStatus){
            this.editApplyForm.approver='审核人';
            this.editApplyForm.approvalStatus=approvalStatus;
            await approvalApply(this.editApplyForm);
            this.resetFields();
        },
        //审核跟踪
        async approvalDetail(apply){
            this.editFormVisible = true;
            this.title="审核跟踪";
            let response =await searchOne(apply.applyId);
            this.editApplyForm=JSON.parse(JSON.stringify(response));
            this.editApplyForm.approvalTime=this.changeDate(this.editApplyForm.approvalTime);
            if(response.sellCode!==null){
                this.orderDetail(response.sellCode);
            }
        },
        //入库订单生成
        async addInorder(){
            await applyToInorder(this.inorderList);
            this.findPage();
        },
        //复选框的change事件，删除和修改拿到id
        checkBoxSelectionChange(selection){
            this.inorderList = selection;
        },
        currentPageChange(currentPage){
            this.searchParams.currentPage = currentPage;
            //console.count(this.searchParams.currentPage);
            this.findPage();
        },
        //表格日期转换
        formatDate(row, column){
            let date = row[column.property];
            return this.changeDate(date);
        },
        //日期转换
        changeDate(date){
            if(date === null){
                return date;
            }
            let one = date.indexOf('-');
            let year = date.slice(0,one);
            let month = date.slice(one+1,one+3);
            let day = date.slice(one+4,one+6);
            let time = date.slice(date.indexOf('T')+1);
            return year+'年'+month+'月'+day+'日'+' '+time;
        },
       async findPage(){
           let response =  await searchPage(this.searchParams);
               this.total= response.total;
               this.tableData = response.data;
        },
        changeStatus(num){
            this.searchParams.approvalStatus = num;
            this.findPage();
        },
        chooseCreateTime(){
            this.searchParams.startTime=this.createTimeOptions[0];
            this.searchParams.endTime=this.createTimeOptions[1];
        },
        chooseApprovalTime(){
            this.searchParams.approvalStartTime=this.approvalTimeOptions[0];
            this.searchParams.approvalEndTime=this.approvalTimeOptions[1];
        },
        resetForm(){
            this.searchParams={ currentPage :1, pageSize:5,approvalStatus:0};
            this.createTimeOptions ='';
            this.approvalTimeOptions ='';
        },
        resetFields(){
            this.editFormVisible = false;
            this.editForm = true;
            this.orderData={};
            this.editApplyForm={
                applyId:'',
                    orderId:'',
                    complaintId:'',
                    producer:'',
                    producDept:'售后部门',
                    remarks:'',
                    status:0,
                    overhaulId:'',
                    overhaulName:'',
                    overhaulTime:'',
                    overhaulPhone:'',
                    approver:'',
                    approvalTime:'',
                    approvalIdea:'',
                    approvalStatus:0,
                    createTime:'',
                    entryId:'',
                    list:[]
            };
            this.findPage();
        }
    },
    created(){
        this.findPage();
    }
};

export default approval;